
/**
* @Description: 上传文件的校验对象大小
* @author maoze@hsyuntai.com
* @date 2018/2/8 16:21
* @params arget 文件上传的DOM对象
* @params filemaxsize 最大的文件上size
* @params ploadingError 上传失败的提示
* @return   true/false
* Modified By:
*/
var isIE = /msie/i.test(navigator.userAgent) && !window.opera;
function fileChange(id, filemaxsize,uploadingError) {
    var  target = document.getElementById(id);
    var fileSize = 0;
    var filemaxsize = filemaxsize;
    if($(target).val() != ''){
        if (isIE && !target.files) {
            var filePath = target.value;
            var fileSystem = new ActiveXObject("Scripting.FileSystemObject");
            if (!fileSystem.FileExists(filePath)) {
                alert("附件不存在，请重新输入！");
                return false;
            }
            var file = fileSystem.GetFile(filePath);
            fileSize = file.Size;
        } else {
            fileSize = target.files[0].size;
        }
        var size = fileSize / 1024;
        if (size > filemaxsize) {
            uploadingError();
            return false;
        }
        return true;
    }
}

/**
* @Description:
* @author maoze@hsyuntai.com
* @date 2018/2/8 17:17
* @params ids id的数组
* @params filemaxsize size一个统一值
* @params uploadingError 返回函数一个统一值
* @return  true/false
* Modified By:
*/
function fileChanges(ids, filemaxsizes,uploadingError) {
     for(var i =0 ; i < ids.length ;  i++){
         var  target = document.getElementById(ids[i]);
         var fileSize = 0;
         var filemaxsize = 0;
         if(filemaxsizes != null && filemaxsizes.length == 1){
              filemaxsize =filemaxsizes ;
         }else if(filemaxsizes != null && filemaxsizes.length > 1){
              filemaxsize =filemaxsizes[i] ;
         }
         if($(target).val() != ''){
             if (isIE && !target.files) {
                 var filePath = target.value;
                 var fileSystem = new ActiveXObject("Scripting.FileSystemObject");
                 if (!fileSystem.FileExists(filePath)) {
                     alert("附件不存在，请重新输入！");
                     return false;
                 }
                 var file = fileSystem.GetFile(filePath);
                 fileSize = file.Size;
             } else {
                 fileSize = target.files[0].size;
             }
             var size = fileSize / 1024;
             if (size > filemaxsize) {
                  uploadingError();
                 return false;
             }
             return true;
         }
     }
}

/**
 * @Description: 文件校验
 * @Return: 
 * @Param: id 文件id
 * @Param: imageId 图片缩略图id
 * @Param: fileMaxSize 对应文件的大小，如10k表示1024 * 10
 * @Param: fileHeight 对应文件的高度限制，如100
 * @Param: fileWidth 对应文件的宽度限制，如100
 * @Param: fileMaxSizeErrorMsg 文件大小超过时文案
 * @Param: fileHeightOrWidthErrorMsg 文件尺寸超过时文案
 * @Author: caiyl@hsyuntai.com
 * @Date: 2018/3/5 14:47
 */
function fileCheck(id, imageId, fileMaxSize, fileHeight, fileWidth, fileMaxSizeErrorMsg, fileHeightOrWidthErrorMsg, saveFun) {
    if(!fileMaxSizeErrorMsg){
        //使用默认文案提示
        fileMaxSizeErrorMsg = "您上传的图片大小超出限制范围，无法上传，请重新上传符合要求的图片！";
    }
    if(!fileHeightOrWidthErrorMsg){
        //使用默认文案提示
        fileHeightOrWidthErrorMsg = "您上传的图片尺寸不符合要求，可能会影响APP展示效果，是否继续保存？";
    }
    var target = document.getElementById(id);
    var fileSize = 0;
    if($(target).val() != ''){
        if (isIE && !target.files) {
            var filePath = target.value;
            var fileSystem = new ActiveXObject("Scripting.FileSystemObject");
            if (!fileSystem.FileExists(filePath)) {
                console.log("文件不存在！");
            }
            var file = fileSystem.GetFile(filePath);
            fileSize = file.Size;
        } else {
            fileSize = target.files[0].size;
        }
        if(fileSize == 0){
            excuteSaveFun(saveFun);
            return;
        }
        if (fileSize > fileMaxSize) {
            //给出错误提示
            swal({
                title:"",
                text:fileMaxSizeErrorMsg,
                showCancelButton:false,
                showConfirmButton:true,
                confirmButtonText:"确定",
                // cancelButtonText:"取消",
                animation:"slide-from-top"
            }, function() {
                return true;
            });
            return false;
        }
        if(fileHeight && fileWidth && imageId ){
            var myImage = document.getElementById(imageId);
            var rw = myImage.naturalWidth; // 真实图片宽度
            var rh = myImage.naturalHeight; //真实图片高度
            if(rw != fileWidth || rh != fileHeight){
                //给出错误提示
                swal({
                    title:"",
                    text:fileHeightOrWidthErrorMsg,
                    showCancelButton:true,
                    showConfirmButton:true,
                    confirmButtonText:"是",
                    cancelButtonText:"否",
                    animation:"slide-from-top"
                }, function() {
                    excuteSaveFun(saveFun);
                });
            } else {
                //图片合法
                excuteSaveFun(saveFun);
            }
            return false;
        }
        excuteSaveFun(saveFun);
    } else {
        excuteSaveFun(saveFun);
    }
}

function excuteSaveFun(saveFun) {
    if(saveFun){
        saveFun();
    }
}

function fileSizeCheck(id, fileMaxSize, fileMaxSizeErrorMsg, saveFun) {
    if(!fileMaxSizeErrorMsg){
        //使用默认文案提示
        fileMaxSizeErrorMsg = "您上传的图片大小超出限制范围，无法上传，请重新上传符合要求的图片！";
    }
    var target = document.getElementById(id);
    var fileSize = 0;
    if($(target).val() != ''){
        if (isIE && !target.files) {
            var filePath = target.value;
            var fileSystem = new ActiveXObject("Scripting.FileSystemObject");
            if (!fileSystem.FileExists(filePath)) {
                console.log("文件不存在！");
            }
            var file = fileSystem.GetFile(filePath);
            fileSize = file.Size;
        } else {
            fileSize = target.files[0].size;
        }
        if(fileSize == 0){
            excuteSaveFun(saveFun);
            return;
        }
        if (fileSize > fileMaxSize) {
            //给出错误提示
            swal({
                title:"",
                text:fileMaxSizeErrorMsg,
                showCancelButton:false,
                showConfirmButton:true,
                confirmButtonText:"确定",
                animation:"slide-from-top"
            }, function() {
                return true;
            });
            return false;
        }
        excuteSaveFun(saveFun);
    } else {
        excuteSaveFun(saveFun);
    }
}

//校验多张图片的大小
function filesSizeCheck(idList, sizeList, msgList, saveFun) {
    if (!idList || idList.length == 0 || !sizeList || sizeList.length == 0 || !msgList || msgList.length == 0) {
        excuteSaveFun(saveFun);
        return;
    }
    for (var i=0; i<idList.length; i++) {
        var target = document.getElementById(idList[i]);
        var fileSize = 0;
        if($(target).val() != ''){
            if (isIE && !target.files) {
                var filePath = target.value;
                var fileSystem = new ActiveXObject("Scripting.FileSystemObject");
                if (!fileSystem.FileExists(filePath)) {
                    console.log("文件不存在！");
                }
                var file = fileSystem.GetFile(filePath);
                fileSize = file.Size;
            } else {
                fileSize = target.files[0].size;
            }
            if(fileSize == 0){
                continue;
            }
        }
        if (fileSize > sizeList[i]) {
            //给出错误提示
            swal({
                title:"",
                text:msgList[i],
                showCancelButton:false,
                showConfirmButton:true,
                confirmButtonText:"确定",
                animation:"slide-from-top"
            }, function() {
                return true;
            });
            return false;
        }
    }
    excuteSaveFun(saveFun);
}

//脚丫多张图片的尺寸
function filesChiCunCheck(imageList, saveFun) {
    if (!imageList || imageList.length == 0) {
        excuteSaveFun(saveFun);
        return;
    }
    var nameStr = '';
    var sizeStr = '';
    for (var i=0; i<imageList.length; i++) {
        var imageInfo = imageList[i];
        var fileId = imageInfo.fileId;
        var target = document.getElementById(fileId);
        var fileSize = 0;
        if($(target).val() != ''){
            if (isIE && !target.files) {
                var filePath = target.value;
                var fileSystem = new ActiveXObject("Scripting.FileSystemObject");
                if (!fileSystem.FileExists(filePath)) {
                    console.log("文件不存在！");
                }
                var file = fileSystem.GetFile(filePath);
                fileSize = file.Size;
            } else {
                fileSize = target.files[0].size;
            }
        }
        if (fileSize != 0) {
            var myImage = document.getElementById(imageInfo.imageId);
            var rw = myImage.naturalWidth; // 真实图片宽度
            var rh = myImage.naturalHeight; //真实图片高度
            var fileWidth = imageInfo.fileWidth;
            var fileHeight = imageInfo.fileHeight;
            if (rw != fileWidth || rh != fileHeight) {
                nameStr += '【'+ imageInfo.imageName +'】';
                sizeStr += '['+ fileWidth + '*' + fileHeight +'px]';
            }
        }
    }
    if (sizeStr != '' && nameStr != '') {
        var msg = '您上传的图片' + nameStr + '尺寸不符合要求' + (imageList.length == 1 ? '':'，依次是') + sizeStr + '，可能会影响APP展示效果，是否继续保存？';
        //给出错误提示
        swal({
            title:"",
            text:msg,
            showCancelButton:true,
            showConfirmButton:true,
            confirmButtonText:"是",
            cancelButtonText:"否",
            animation:"slide-from-top"
        }, function() {
            excuteSaveFun(saveFun);
        });
    } else {
        //图片合法
        excuteSaveFun(saveFun);
    }
    return false;
}



//校验多张图片的大小
function filesSizeCheckWithForceFlag(idList, sizeList, msgList, saveFun,isForce) {
    if (!idList || idList.length == 0 || !sizeList || sizeList.length == 0 || !msgList || msgList.length == 0) {
        excuteSaveFun(saveFun);
        return;
    }
    var flag = false,msg = '您上传的';;
    for (var i=0; i<idList.length; i++) {
        var target = document.getElementById(idList[i]);
        var fileSize = 0;
        if($(target).val() != ''){
            if (isIE && !target.files) {
                var filePath = target.value;
                var fileSystem = new ActiveXObject("Scripting.FileSystemObject");
                if (!fileSystem.FileExists(filePath)) {
                    console.log("文件不存在！");
                }
                var file = fileSystem.GetFile(filePath);
                fileSize = file.Size;
            } else {
                fileSize = target.files[0].size;
            }
            if(fileSize == 0){
                continue;
            }
        }
        if (fileSize > sizeList[i]) {
            flag = true;
            msg += msgList[i]+",";
        }
    }
    msg += "不符合要求，请重新上传符合要求的图片！"
    if(isForce){
        if(flag){
            // 出现强制提示
            toastr.error(msg);
            return false;
        }
            return true;
      }else{
        if(flag){
          //给出错误提示
          swal({
              title:"",
              text:msg,
              showCancelButton:true,
              showConfirmButton:true,
              confirmButtonText:"确定",
              cancelButtonText:"我再改改",
              animation:"slide-from-top"
          }, function() {
              return true;
          });
        }
          return false;
      }

    excuteSaveFun(saveFun);
}